﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWMAG.Scripting.AST.ObjectModel;
using IWMAG.Scripting.Types;
using IWMAG.Scripting.AST.TerminalExpressions;
using IWMAG.Scripting.Interpreter;
using IWMAG.Scripting.Compiler.UserInfo;

namespace IWMAG.Scripting.AST.Statements {
    internal class LogStatement : CodeStatement {
        public CodeExpression Value;

        public LogStatement(Compiler.Lexeme l, CodeExpression val)
            : base(l) {
            Value = val;
        }

        public override void Verify(Helpers.CompileTable sym, List<Compiler.IUserInfo> error) {
            Value.Verify(sym, error);

            if (Value.TypeOf().Equals(CodeType.Void)) {
                SendError(error, Rules.LogStatementCannotLogVoidValue);

            }
        }

        public override IEnumerable<Continuation> Execute(Interpreter.ScriptEngine engine, Helpers.RuntimeTable sym) {
            Reference<RunValue> val = RunValue.NoValue;
            foreach (Continuation wait in Value.ValueOf(engine, sym, val)) yield return wait;

            string s = val.ReferencedValue.ToString();
            engine.Writer.WriteLine(s);
            engine.Writer.Flush();

            yield break;
        }
    }
}
